package com.yanqu.road.dao.impl.activity.stronghold;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.StrongholdSilverDonate;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class StrongholdSilverDonateDaoImpl extends TempDataObjectDao<StrongholdSilverDonate> {
    public static String getStrongholdSilverDonateSql = "select * from t_u_activity_stronghold_silver_donate where activityId=?";
    public Map<String, Map<Long, StrongholdSilverDonate>> getStrongholdSilverDonate(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, Map<Long, StrongholdSilverDonate>> dataMap = new ConcurrentHashMap<>(8);
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getStrongholdSilverDonateSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdSilverDonate strongholdSilverDonate = parse(rs);
                String unionUid = strongholdSilverDonate.getUnionUid();
                if (!dataMap.containsKey(unionUid)) {
                    dataMap.put(unionUid, new ConcurrentHashMap<>(8));
                }
                Map<Long, StrongholdSilverDonate> donateMap = dataMap.get(unionUid);
                donateMap.put(strongholdSilverDonate.getUserId(), strongholdSilverDonate);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateStrongholdSilverDonateSql = "update t_u_activity_stronghold_silver_donate set num=? where activityId=? and unionUid=? and userId=?";
    @Override
    protected boolean updateData(StrongholdSilverDonate strongholdSilverDonate) {
        strongholdSilverDonate.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdSilverDonate.getNum().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdSilverDonate.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdSilverDonate.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdSilverDonate.getUserId()));
        if (getDb().executeNoneQuery(updateStrongholdSilverDonateSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertStrongholdSilverDonateSql = "insert into t_u_activity_stronghold_silver_donate(activityId,unionUid,userId,num) values (?,?,?,?)";
    @Override
    protected boolean addData(StrongholdSilverDonate strongholdSilverDonate) {
        strongholdSilverDonate.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdSilverDonate.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdSilverDonate.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdSilverDonate.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdSilverDonate.getNum().toString()));
        if (getDb().executeNoneQuery(insertStrongholdSilverDonateSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected StrongholdSilverDonate parseData(ResultSet rs) throws SQLException {StrongholdSilverDonate strongholdSilverDonate = new StrongholdSilverDonate();
        strongholdSilverDonate.setActivityId(rs.getInt("activityId"));
        strongholdSilverDonate.setUnionUid(rs.getString("unionUid"));
        strongholdSilverDonate.setUserId(rs.getLong("userId"));
        strongholdSilverDonate.setNum(new BigInteger(rs.getString("num")));

        return strongholdSilverDonate;
    }

}
